package coc.core;

import java.io.InputStream;
import java.util.List;
import java.util.concurrent.FutureTask;

import org.junit.Test;

import coc.core.Crawler;
import coc.core.CrawlerConfig;
import coc.core.CrawlerResult;
import coc.core.SpiderResult;
import coc.core.collector.CollectorType;
import coc.core.url.Url;
import coo.base.util.FileUtils;

public class CrawlerTestForHtmlUnit {
	@Test
	public void test() throws Exception {
		TestCrawler crawler = new TestCrawler(getConfig());
		FutureTask<CrawlerResult> task = new FutureTask<CrawlerResult>(crawler);
		task.run();
		task.get();
	}

	private CrawlerConfig getConfig() {
		String url = "http://epaper.oeeee.com";

		CrawlerConfig config = new CrawlerConfig();
		config.setTaskId("Task-001");
		config.setTaskName("测试任务");
		config.setStartUrl(new Url(url));
		config.setScript(getScript());
		config.addCollectorTerm(
				"$url.toString().equals(\"http://epaper.oeeee.com\")",
				CollectorType.HTMLUNIT.toString());
		config.setThreadCount(20);
		config.setJobTimeout(60);
		config.setTimeout(5);
		config.setMaxDepth(2);
		return config;
	}

	private String getScript() {
		StringBuilder builder = new StringBuilder();
		builder.append("this.interpreter.set(\"$domain\", \"epaper\");");
		builder.append("this.interpreter.set(\"$text\", \".wqbcontentcss\");");
		builder.append("this.interpreter.set(\"$author\", \".font_12_666666_h26_center>a\");");
		builder.append("this.interpreter.set(\"$title\", \".font_26_333333_h40_center\");");
		builder.append("this.interpreter.set(\"$publishDate\", \".font_12_666666_h26_center\");");

		InputStream in = getClass().getResourceAsStream(
				"CrawlerTestForHtmlUnit.bsh");
		return builder.append(new String(FileUtils.toByteArray(in))).toString();
	}

	private class TestCrawler extends Crawler {
		public TestCrawler(CrawlerConfig config) {
			super(config);
		}

		@Override
		protected List<Url> filterUrls(String taskId, List<Url> suspendUrls) {
			return suspendUrls;
		}

		@Override
		protected Integer saveSpiderResult(SpiderResult spiderResult) {
			log.debug(spiderResult.getData().toString());
			return spiderResult.getDataSize();
		}

		@Override
		protected void complete() {
			log.debug("完成");
		}
	}
}
